package com.example.megaandroidrssreader;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

public class RssDetailActivity extends BaseActivity {
	
	private TextView txtTitle;
	private TextView txtPubDate;
	private TextView txtFullText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rss_detail);
		
		Intent intent = this.getIntent();
		
		txtTitle = (TextView) this.findViewById(R.id.txtDetailRssTitle);
		txtTitle.setText(intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.title"));

		this.onUpdateViewMode();
		
		// Checking if RssItem.link or RssItem.pdaLink that got from intent bundle are not empt
		// so changing style of title TextView to blue underlined. Link stored in linkString
		String linkString = null; 
		if (intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.pdalink").length() > 0) {
			linkString = intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.pdalink");
			txtTitle.setTextColor(Color.BLUE);
			txtTitle.setPaintFlags(txtTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
		}
		else
			if (intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.link").length() > 0) {
				linkString = intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.link");
				txtTitle.setTextColor(Color.BLUE);
				txtTitle.setPaintFlags(txtTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
			}
			else 
				txtTitle.setTextColor(Color.BLACK);

		// If linkString is not empty then adding OnClickListener that will open that link
		if (linkString.length() > 0) {
			final String finalLinkString = linkString;
			txtTitle.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					RssDetailActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(finalLinkString)));
				}
			});
		}

		txtPubDate = (TextView) this.findViewById(R.id.txtDetailRssPubDate);
		txtPubDate.setText(intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.pubdate"));

		txtFullText = (TextView) this.findViewById(R.id.txtDetailRssFullText);
		txtFullText.setText(intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.fulltext"));
		
		String link = intent.getExtras().getString(ItemsActivity.PACKAGE_NAME+".RssItem.link");
		
		WebView webHtmlView = (WebView) this.findViewById(R.id.webHtmlView);
		if (link.length() > 0) {
			webHtmlView.loadUrl(link);
		}
			
	}
	
	@Override
	protected void onUpdateViewMode() {
		super.onUpdateViewMode();
		ViewGroup layoutPlainText = (ViewGroup) this.findViewById(R.id.layoutPlainText);
		ViewGroup layoutHtml = (ViewGroup) this.findViewById(R.id.layoutHtml);
		if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_HTML) {
			layoutPlainText.setVisibility(View.INVISIBLE);
			layoutHtml.setVisibility(View.VISIBLE);
		}
		if (mAppSettings.mViewMode == AppSettings.VIEW_MODE_PLAIN_TEXT) {
			layoutPlainText.setVisibility(View.VISIBLE);
			layoutHtml.setVisibility(View.INVISIBLE);
		}
	}
	
}
